<template>
  <view class="car-model-list">
    <TagList />

    <CarModelItem v-for="record in dataSource" :record="record" :key="record.id" @toDetail="toDetail" />
    <!-- 加载结束列表为空时展示 -->
    <ve-empty v-if="dataSource.length == 0 && loading == false"></ve-empty>
    <!-- 加载中tip -->
    <ve-toast ref="toastRef"></ve-toast>
  </view>
</template>

<script setup>
  import { ref } from 'vue'
  import { getCarModelStock } from '@/api/asset/car-stock'
  import CarModelItem from './component/car-model-item.vue'
  import TagList from '../components/default-tag-list.vue'

  const props = defineProps({
    selectable: {
      type: Boolean,
    },
  })

  const dataSource = ref([])
  const toastRef = ref()
  const loading = ref(true)
  let queryParam
  const loadData = (qParam) => {
    queryParam = {
      siteId: qParam.siteId,
      seriesIdList: qParam.seriesIdList,
      modelIdList: qParam.modelIdList,
      startTime: qParam.startTime,
      endTime: qParam.endTime,
    }
    toastRef.value.showLoading()
    getCarModelStock(queryParam)
      .then((res) => {
        uni.stopPullDownRefresh()
        if (res.code == 200) {
          const result = res.result
          dataSource.value = result.list
        }
      })
      .finally(() => {
        toastRef.value.hideLoading()
        loading.value = false
      })
  }

  // 查看详情
  const toDetail = (record) => {
    let params = {
      ...queryParam,
      modelId: record.id,
    }
    uni.navigateTo({
      url: `/pages/asset/stock/model-stock/detail?params=${JSON.stringify(params)}`,
    })
  }

  defineExpose({ loadData })
</script>
